
/**
 * @author a. lothore
 * @classDescription ServerInterface object : remote calls
 */

function ServerInterface()
{
	this.dataSource = defaultSettings.dataSource;
	this.callMethod = defaultSettings.callMethod; //iframe, xmlhttp or ondemandjs
	this.dataFormat = defaultSettings.dataFormat; //JSON ou XML
	this.iframe 	= null;

	this.setDataSource 	= ServerInterface_setDataSource;
	this.getDataSource 	= ServerInterface_getDataSource;	
	this.setCallMethod 	= ServerInterface_setCallMethod;
	this.getCallMethod 	= ServerInterface_getCallMethod;
	this.remoteCall 	= ServerInterface_remoteCall;
	this.addHiddenInput = ServerInterface_addHiddenInput;
}

//--------------------------------------------------------------------------------------------------------------------------
function ServerInterface_setDataSource(url)
{
	this.dataSource = url || this.dataSource;
}

//--------------------------------------------------------------------------------------------------------------------------
function ServerInterface_getDataSource()
{
	return this.dataSource;
}

//--------------------------------------------------------------------------------------------------------------------------
function ServerInterface_setCallMethod(callMethod)
{
  this.callMethod = callMethod || this.callMethod;
}

//--------------------------------------------------------------------------------------------------------------------------
function ServerInterface_getCallMethod()
{
	return this.callMethod;
}

//--------------------------------------------------------------------------------------------------------------------------
function ServerInterface_remoteCall(action,params,client_id,formToSend)
{
	var actionParam = "";
	
	if (this.dataSource.indexOf("?") != -1) 
    	{actionParam = "action="+action+"&";}
	else 
    	{actionParam = "/"+action+"?";}
	
	if (!formToSend)
	{
    	if (this.callMethod == "xmlhttp")
    	{
      		var url;
      		if (typeof(picasaConnect)!='undefined' && picasaConnect.isPicasaRequest(this.dataSource))
			{
				url = replaceStr(this.dataSource,"alt=rss","alt=json") + "&callback=?";
				
				$.getJSON(url,
					function (json){
					  getGUI(client_id).onRemoteCall(picasaConnect.transformPicasaResult(json));
					});
			}
			else      		
			{
				url = this.dataSource+"&"+actionParam+params+"&callmethod="+this.callMethod+"&connectedUser="+connectedUser.login+"&pass="+connectedUser.mdp
				
				$.getJSON(url,
					function (json){
					  getGUI(client_id).onRemoteCall(json);
					});
			}
    	}
    	else
    	{
      		if (this.callMethod == "ondemandjs")
      		{
				if (typeof(picasaConnect)!='undefined' && picasaConnect.isPicasaRequest(this.dataSource))
				{ 
				  alert('Not implemented : use xmlhttp instead');
				}
					else
				{
				  var scriptSrc = this.dataSource+"&"+actionParam+params+"&callmethod="+this.callMethod+"&connectedUser="+connectedUser.login+"&pass="+connectedUser.mdp;
				  $(document).append("<script type=\"text/javascript\" src=\""+scriptSrc+"\"></script>");
				}
      		}
    	}
  	}
  	else
  	{
		this.addHiddenInput(formToSend,'client_id',client_id);
		this.addHiddenInput(formToSend,'callmethod','iframe');
		this.addHiddenInput(formToSend,'connectedUser',connectedUser.login);
		this.addHiddenInput(formToSend,'pass',connectedUser.mdp); //TODO : Secure this ??
		formToSend.action = this.dataSource+actionParam;		
		formToSend.submit();
	}
}

//--------------------------------------------------------------------------------------------------------------------------
function ServerInterface_addHiddenInput(formToSend, name, value)
{
    var jhInput = $("input[name='"+name+"']",formToSend);
		if (jhInput.size()>0)
		{
      jhInput.slice(0).val(value);
		}
		else
		{
      var hInput = document.createElement('input');
      hInput.type = 'hidden';
      hInput.name = name;
      hInput.value = value;
      formToSend.appendChild(hInput);
    }
}		
		